import json
from datetime import datetime
import requests
from urllib.parse import quote, unquote



with open('cookies.json', 'r', encoding='utf-8') as f:
    cookies = json.load(f)
    headers_dd = cookies['调度']
    headers_sl = cookies['受理']
def resend_sl(datas):
    lists=[]
    for i in datas:
        lists.append({
            "bsNum": f"{i[0]}",
            "receiveNumber": f"{i[1]}",
            "itemCode": f"{i[2]}",
            "bsType": "accept"
        })
    strs=quote(quote(str(lists), safe=':/?&='))
    data = {
        "arrayStr": strs
 }

    response = requests.post('http://59.215.206.188/zhslzy/accept/business/accept/resend',
                             headers=headers_sl,
                             data=data)
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    print(response.text)

def query_dd(data):
    datas=[]
    for i in data:

        data={
            "bsNum": i[0],
            "_search": "true",
            "nd": datetime.now().timestamp()*1000,
            "rows": "10",
            "page": "1",
            "sidx": "",
            "sord": "asc"
        }
        response = requests.post(
            'http://59.215.206.188/ddglzy/dispatch/dockingcenter/call/queryByBsnum',

            headers=headers_dd,
            data=data,
        ).json()
        if response['total']==0:
            datas.append(i)

    resend_sl(datas)


# 使用Celery任务装饰器标记此函数为Celery任务

def query_sl():
    data={
    "applicant": "",
    "sblsh": "",
    "submitTime": "2025-10-01+至+2025-10-10",
    "timeStart": "2025-10-01",
    "timeEnd": "2025-10-10",
    "sxmc": "",
    "regionName": "",
    "regionCode": "",
    "orgName": "",
    "orgCode": "",
    "_search": "true",
    "nd": datetime.now().timestamp()*1000,
    "rows": "500",
    "page": "1",
    "sidx": "RECEIVE_TIME",
    "sord": "desc"
}
    response = requests.post(
        'http://59.215.206.188/zhslzy/accept/business/accept/queryAccept',

        headers=headers_sl,
        data=data,
    ).json()
    datas=[]
    for i in response['rows']:
        datas.append([i['cell']['BSNUM'],i['cell']['RECEIVE_NUMBER'],i['cell']['ITEM_CODE']])

    query_dd(datas)


# query_dd('520325511GG0150525092200002')
# 注意：以下调用应注释掉，因为Celery会负责调度执行任务
# query_sl()
# resend_sl()